package com.sxt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.annos.DeleteCache;
import com.sxt.annos.StartCache;
import com.sxt.domain.User;
import com.sxt.mapper.UserMapper;
import com.sxt.model.PageBean;
import com.sxt.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Override
	public Integer add(User user) {
		Assert.notNull(user, "新增时对象不能为null");
		return userMapper.insert(user);
	}

	@Override
	@DeleteCache(prefix="com.sxt.domain.User",params="#0")
	public Integer delete(Integer id) {
		Assert.notNull(id,"删除对象时id不能为null");
		return userMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Integer update(User user) {
		Assert.notNull(user,"修改对象时user不能为null");
		return userMapper.updateByPrimaryKey(user);
	}

	@Override
	@StartCache(prefix="com.sxt.domain.User",params="#0")
	public User findById(Integer id) {
		Assert.notNull(id,"查询对象时id不能为null");
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	@StartCache(prefix="com.sxt.domain.User",params="#0#1")
	public PageBean<User> findAll(int currentPage, int size) {
		Page<Object> page = PageHelper.startPage(currentPage, size);
		List<User> users = userMapper.selectByExample(null);
		long total = page.getTotal();
		return new PageBean<>(currentPage, size, total, users);
	}

}
